<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 通过CURL发送HTTP请求
 * @param string $url  //请求URL
 * @param array $postFields //请求参数
 * @return mixed
 */
function curlPost($url,$postFields){
    $postFields = http_build_query($postFields);
    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_POST, 1 );
    curl_setopt ( $ch, CURLOPT_HEADER, 0 );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
    $result = curl_exec ( $ch );
    curl_close ( $ch );
    return $result;
}

function curlSmsPost($url='',$data){
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);
    if(!empty($data)){
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
    }
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
    $output = curl_exec($curl);
    curl_close($curl);

    return $output;
}

function execResult($result){
    $result=preg_split("/[,\r\n]/",$result);
    return $result;
}

function json_format($data, $indent=null)
{

	// 对数组中每个元素递归进行urlencode操作，保护中文字符
	array_walk_recursive($data, 'json_format_protect');

	// json encode
	$data = json_encode($data);

	// 将urlencode的内容进行urldecode
	$data = urldecode($data);

	// 缩进处理
	$ret = '';
	$pos = 0;
	$length = strlen($data);
	$indent = isset($indent)? $indent : '    ';
	$newline = "\n";
	$prevchar = '';
	$outofquotes = true;
	for($i=0; $i<=$length; $i++){
		$char = substr($data, $i, 1);
		if($char=='"' && $prevchar!='\\')
		{
			$outofquotes = !$outofquotes;
		}elseif(($char=='}' || $char==']') && $outofquotes)
		{
			$ret .= $newline;
			$pos --;
			for($j=0; $j<$pos; $j++){
				$ret .= $indent;
			}
		}
		$ret .= $char;
		if(($char==',' || $char=='{' || $char=='[') && $outofquotes)
		{
			$ret .= $newline;
			if($char=='{' || $char=='['){
				$pos ++;
			}

			for($j=0; $j<$pos; $j++){
				$ret .= $indent;
			}
		}
		$prevchar = $char;
	}
	return $ret;
}

/**
 * json_formart辅助函数
 * @param String $val 数组元素
 */
function json_format_protect(&$val)
{
	if($val!==true && $val!==false && $val!==null)
	{
		$val = urlencode($val);
	}
}


    /**
      * 获取推流地址
      * 如果不传key和过期时间，将返回不含防盗链的url
      * @param bizId 您在腾讯云分配到的bizid
      *        streamId 您用来区别不通推流地址的唯一id
      *        key 安全密钥
      *        time 过期时间 sample 2016-11-12 12:00:00
      * @return String url */
            function getPushUrl($bizId, $streamId, $key = null, $time = null){
            
                if($key && $time){
                    $txTime = strtoupper(base_convert(strtotime($time),10,16));
                    //txSecret = MD5( KEY + livecode + txTime )
                    //livecode = bizid+"_"+stream_id  如 8888_test123456
                    $livecode = $bizId."_".$streamId; //直播码
        $txSecret = md5($key.$livecode.$txTime);
                    $ext_str = "?".http_build_query(array(
                        "bizid"=> $bizId,
                        "txSecret"=> $txSecret,
                        "txTime"=> $txTime
                    ));
                }
                return "rtmp://".$bizId.".livepush.myqcloud.com/live/".$livecode.(isset($ext_str) ? $ext_str : "");
            }
            
            // echo getPushUrl("8888","123456","f4e245d1b010b920076ab2d9d0308dae  ","2017-10-12 23:59:59");
            // echo "<br />";
            /**
      * 获取播放地址
      * @param bizId 您在腾讯云分配到的bizid
      *        streamId 您用来区别不通推流地址的唯一id
      * @return String url */
            function getPlayUrl($bizId, $streamId){
                $livecode = $bizId."_".$streamId; //直播码
    return array(
                    "rtmp://".$bizId.".liveplay.myqcloud.com/live/".$livecode,
                    "http://".$bizId.".liveplay.myqcloud.com/live/".$livecode.".flv",
                    "http://".$bizId.".liveplay.myqcloud.com/live/".$livecode.".m3u8"
                );
            }
            // print_r(getPlayUrl("8888","123456"));
            